#include "InterpolationSolver.h"
#include <iostream>
#include <fstream>
#define pi acos(-1)

//[题目C要求]Reuse your subroutine of Newton interpolation to perform Chebyshev interpolation for the function. Clearly the Runge function f(x) is a scaled version of the function in B. Plot the interpolating polynomials against the exact function to observe that the Chebyshev interpolation is free of the wide oscillations in the previous assignment.

class Fun : public Function
{
public:
    double operator()(double _x)
    {
        double y;
        y = 1/(1 + 25.0*_x*_x);
        return y;
    }
};


int main()
{
    std::ofstream fout;
    fout.open("proC.txt");
    Fun f;

    for (int k = 1; k < 5; k++)
    {
        std::vector<double> v;
        int n = 5*k;
        for (int i = 1; i <= n ; i++)
        {
            v.push_back(cos(pi*(2*i - 1)/(2*n)));
        }
        Newton_Formula b(f, v);
        fout << "x=-1:0.002:1;" << std::endl;
        fout << "y=" << "[";
        for (int i = 0; i < 1000; i++)
        {
            fout << b.solve(-1.0 + 2.0*i/1000) << ", ";
        }
        fout << b.solve(1.0) << "];" << std::endl;
        fout << "plot(x,y,'*');" << std::endl;
    }

    fout.close();
    return 0;
}
